package thread;

public class YieldDemo implements Runnable {

    @Override
    public void run() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName() + ": " + i);
            //线程的交替顺序不一定能得到保证
            Thread.yield();
        }
    }

    public static void main(String[] args) {
        YieldDemo yieldDemo = new YieldDemo();
        Thread t1 = new Thread(yieldDemo, "线程1");
        Thread t2 = new Thread(yieldDemo, "线程2");
        t1.start();
        t2.start();
    }

}
